package br.ufrj.dcc.ad.stats;

import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;

import br.ufrj.dcc.ad.ADSimProperties;
import br.ufrj.dcc.ad.SimpleLineChart;
import br.ufrj.dcc.ad.Simulator;
import br.ufrj.dcc.ad.event.AckReceivedEvent;
import br.ufrj.dcc.ad.event.Event;
import br.ufrj.dcc.ad.model.TxTCP;

public class CwndMssStats extends Statistics {
	
	private Map<Double, Double> map;
	
	private Integer sessionId;

	public CwndMssStats(String title, String xLabel, String yLabel, Integer sessionId) {
		super(title, xLabel, yLabel);
		this.sessionId = sessionId;
		this.map = new TreeMap<Double, Double>();
	}

	@Override
	protected void doStatistic(Event event) {
		if (!(event instanceof AckReceivedEvent)) return;
		
		final TxTCP txTCP = Simulator.getTransmitters().get(this.sessionId);
		final Double cwnd = txTCP.getCwnd();
		final Long packageSize = ADSimProperties.getMSS();
		final Double cwndMss = (cwnd / packageSize);
		final Double time = Simulator.getTime();
		
		map.put(time, cwndMss);
	}

	@Override
	protected void finishStatistic() {
		final SimpleLineChart chart = new SimpleLineChart(getTitle(), getxLabel(), getyLabel(), this.map);
		try {
			chart.saveAsImage(getTitle().replaceAll(" ", ""));
		} catch (IOException e) {
			logger.error("Falha ao salvar grafico.", e);
		}
	}

}
